export default {
	data() {
		return {
			options: [{
				text: '删除',
				style: {
					backgroundColor: '#dd524d'
				}
			}, {
				text: '取消',
				style: {
					backgroundColor: '#c7c7c7'
				}
			}, ],
			checkedIdList: [],
			cartList: [],
			priceDetail: {
				totalCount: 0, // 总件数
				totalMoney: 0, // 总原价
				saleDiscount: 0, // 商品优惠
				ticketDiscount: 0, // 优惠券优惠
				totalDiscount: 0, // 总优惠
				needMoney: 0, // 需支付
			}
		}
	},
	async onLoad(options) {
		console.log("cart index onLoad");
	},
	async onShow() {
		if (this.$store.state.login.token) {
			await this.getCartList();
		}
		// this.nowTimeInterval = setInterval(() => {
		// 	this.nowTime = Date.now();
		// }, 1000)
	},
	onHide() {
		// clearInterval(this.nowTimeInterval);
	},
	async onPullDownRefresh() {
		if (this.$store.state.login.token) {
			await this.getCartList();
		}
		uni.stopPullDownRefresh();
	},
	methods: {
		// 购物车
		async getCartList() {
			const cartRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: 'getCartList',
				data: {
					uid: this.$store.state.login.uid
				}
			});
			console.log("cartRes: ", cartRes);
			if (cartRes) {
				this.cartList = cartRes.list || [];
				// this.cartList = [...cartRes.list, ...cartRes.list, ...cartRes.list];
				// if (this.cartList && this.cartList.length > 0) {
				// 	await this.getProductInfo();
				// }
			} else {
				this.cartList = [];
			}
		},
		async getProductInfo() {
			for (let i = 0; i < this.cartList.length; i++) {
				const detailRes = await this.$uniCloudCallFunction({
					name: 'xxf-product',
					action: "getProductDetail",
					data: {
						uid: this.$store.state.login.uid,
						id: this.cartList[i].product_id
					}
				});
				console.log("detailRes: ", detailRes);
				if (detailRes) {
					this.cartList[i].product_info = detailRes || {};
					this.cartList[i].status = '1';
					for (let j = 0; j < detailRes.spu.length; j++) {
						if (detailRes.spu[j].spu_id === this.cartList[i].spuId) {
							this.cartList[i].spu = detailRes.spu[j] || {};
						}
					}
				} else {
					this.cartList[i].status = '-1';
				}
			}
		},
		toDetail(e) {
			if (e.product_info.status === -1) return;
			uni.navigateTo({
				url: `/pages/homePack/proDetail/index?id=${e.product_info._id}`,
			})
		},
		checkboxChange(e) {
			console.log("checkboxChange: ", e);
			const arrVal = e.detail.value;
			this.checkedIdList = arrVal;
			this.doPriceDetail();
		},
		checkAllChange(e) {
			console.log("radioChange: ", e);
			const arrVal = e.detail.value;
			this.checkedIdList = [];
			if (arrVal.length) {
				for (let i = 0; i < this.cartList.length; i++) {
					this.checkedIdList.push(this.cartList[i]._id);
					console.log("checkedIdList: ", this.checkedIdList);
				}
			}
			this.doPriceDetail();
		},
		doPriceDetail() {
			let totalCount = 0,
				totalMoney = 0,
				saleDiscount = 0,
				ticketDiscount = 0,
				totalDiscount = 0,
				needMoney = 0;
			if (this.checkedIdList && this.checkedIdList.length > 0) {
				for (let i = 0; i < this.checkedIdList.length; i++) {
					for (let j = 0; j < this.cartList.length; j++) {
						if (this.checkedIdList[i] === this.cartList[j]._id) {
							const checkItem = this.cartList[j];
							totalCount = checkItem.count + totalCount;
							totalMoney = checkItem.count * checkItem.spu_info.price_original + totalMoney;
							saleDiscount = checkItem.count * (checkItem.spu_info.price_original - checkItem
								.spu_info.price_sale) + saleDiscount;
						}
					}
				}
				// TODO:ticket
				ticketDiscount = 0;
				totalDiscount = saleDiscount + ticketDiscount;
				// TODO:is_vip

				needMoney = totalMoney - totalDiscount;
			}

			this.priceDetail = {
				totalCount,
				totalMoney,
				saleDiscount,
				ticketDiscount,
				totalDiscount,
				needMoney,
			}
		},
		async openSpuPop(e) {
			console.log("openSpuPop: ", e);
			if (e.product_info.status === -1) return;
			this.$refs.spuRef.popupOpen({
				type: "edit",
				cartKey: e.key,
				productInfo: e.product_info,
				spuInfo: e.spu_info,
				count: e.count
			});
		},
		popupChange(e) {
			if (e.show) {
				this.$store.commit('hideTabbar');
			} else {
				this.$store.commit('showTabbar');
			}
		},
		async bookingCart(e) {
			const upRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: 'editCart',
				data: {
					uid: this.$store.state.login.uid,
					cartKey: e.cartKey,
					product_info: e.productInfo,
					spu_info: e.spuInfo,
					count: e.count
				}
			});
			console.log("upRes: ", upRes);
			await this.getCartList();

			this.checkedIdList = [e.cartId];
			this.doPriceDetail();
			await this.forBooking();
		},
		async upCart(e) {
			const upRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: 'editCart',
				data: {
					uid: this.$store.state.login.uid,
					cartKey: e.cartKey,
					product_info: e.productInfo,
					spu_info: e.spuInfo,
					count: e.count
				}
			});
			console.log("upRes: ", upRes);
			this.$utils.showToast(this, {
				message: '购物车已更新！',
				mode: 1
			});
			await this.getCartList();
		},
		forBooking() {
			const proList = [];
			if (this.checkedIdList && this.checkedIdList.length > 0) {
				for (let i = 0; i < this.checkedIdList.length; i++) {
					for (let j = 0; j < this.cartList.length; j++) {
						if (this.checkedIdList[i] === this.cartList[j]._id) {
							proList.push({
								cartKey: this.cartList[j].cartKey,
								productInfo: this.cartList[j],
								spuInfo: this.cartList[j],
								count: this.cartList[j].count,
							})
						}
					}
				}
				uni.navigateTo({
					url: `/pages/homePack/booking/index?proList=${JSON.stringify(proList)}`
				});
			} else {
				this.$utils.showToast(this, {
					message: '请勾选商品！',
					mode: 2
				});
			}
		},
		async delCart(e) {
			console.log("delCart: ", e);
			const delRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: 'deleteCart',
				data: {
					uid: this.$store.state.login.uid,
					cartIds: [e._id],
				}
			});
			console.log("delRes: ", delRes);
			if (delRes) {
				this.$utils.showToast(this, {
					message: '删除成功！',
					mode: 1
				});
				await this.getCartList();

			}
		},
		async onActionClick(e, item) {
			console.log("onActionClick: ", e);
			if (e.content.text === '取消') {
				this.$refs.swipeAction.closeAll();
			}
			if (e.content.text === '删除') {
				console.log('点击了' + (e.position === 'left' ? '左侧' : '右侧') + e.content.text + '按钮');
				await this.delCart(item);
			}
		},
	}
}